import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.dao.ESPojo.ESArticle;
import com.wf.dao.mapper.ArticleBodyMapper;
import com.wf.dao.mapper.ArticleMapper;
import com.wf.dao.mapper.SysUserMapper;
import com.wf.dao.mapper.TagMapper;
import com.wf.dao.pojo.Article;
import com.wf.dao.pojo.ArticleBody;
import com.wf.dao.pojo.SysUser;
import com.wf.scdnApp;
import com.wf.service.ArticleEsService;
import com.wf.service.ArticleService;
import com.wf.vo.ArticleBodyVo;
import com.wf.vo.Result;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@SpringBootTest(classes = scdnApp.class)
public class csdn {

    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleBodyMapper articleBodyMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @Resource
    private ArticleEsService articleEsService;
    @Autowired
    private SysUserMapper sysUserMapper;

    // 测试创建索引
    @Test
    void testCreateIndex() throws IOException {
        boolean success = articleEsService.createIndex();
        System.out.println("创建索引结果：" + success); // 期望 true
    }
    // 测试新增文档
    @Test
    void testAddDocument() throws IOException {
        //先遍历所有文章主题信息
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        List<Article> articles = articleMapper.selectList(queryWrapper);
        for (Article article : articles) {
            ESArticle esArticle = new ESArticle();
            esArticle.setArticleId(article.getId());
            esArticle.setTitle(article.getTitle());
            ArticleBody articleBody = articleBodyMapper.selectById(article.getBodyId());
            esArticle.setContent(articleBody.getContent());
            esArticle.setViewCounts(article.getViewCounts());
            esArticle.setSummary(article.getSummary());
            esArticle.setCreateTime(System.currentTimeMillis());
            SysUser author = sysUserMapper.selectById(article.getAuthorId());
            esArticle.setAuthor(author.getNickname());
//            System.out.println("插入一条信息进入ElasticSearch："+esArticle);
            String docId = articleEsService.addDocument(esArticle);
            System.out.println(docId);
        }
    }
    // 测试分词查询（验证 IK）
    @Test
    void testSearchByKeyword() throws IOException {
        List<ESArticle> articles = articleEsService.searchByKeyword("黄昏");
        System.out.println("查询结果：" + articles.size()); // 期望 1
        articles.forEach(System.out::println);
    }
    @Test
    void testDeleteByKeyWord() throws IOException {
        boolean b = articleEsService.deleteDocument("1");
        System.out.println(b);
    }


    @Test
    public void queryWithoutFirstLevelCache() {
        SqlSession session = sqlSessionFactory.openSession();
        // 执行一些操作
        // session.selectOne("com.example.mapper.UserMapper.selectUserById", 1);

        session.close();
        session = sqlSessionFactory.openSession();
        // 此时新的操作不会使用之前的一级缓存
        // session.selectOne("com.example.mapper.UserMapper.selectUserById", 1);
    }


    @Test
    void prin(){

//        articleMapper.deleteById(15);

        Article article = new Article();
        article.setTitle("测试id自动生成");
        article.setAuthorId(1);
        article.setCategoryId(2);
        article.setCreateDate(System.currentTimeMillis());
        article.setCommentCounts(0);
        article.setSummary("aaa");
        article.setViewCounts(0);
        article.setWeight(0);
        articleMapper.insert(article);
        ArticleBody articleBody = new ArticleBody();
        articleBody.setArticleId(article.getId());
        articleBody.setContentHtml("aaa");
        articleBodyMapper.insert(articleBody);
        article.setBodyId(articleBody.getId());
        System.out.println(article.toString());
        System.out.println(articleBody.toString());
    }

    @Test
    void testArticleService(){
//        Result articleById = articleService.findArticleById(1);
//        Result articleById1 = articleService.findArticleById(21);
//        System.out.println(articleById);
//        System.out.println(articleById1);
        ArticleBodyVo articleBody = findArticleBody(21);
        System.out.println(articleBody);
    }

    @Test
    void testArchiveService(){
        Result result = articleService.listArchives();
        System.out.println(result.getData());
//        System.out.println(System.currentTimeMillis());
    }

    @Test
    void testRedis(){
        System.out.println(redisTemplate.opsForValue().get("java"));
    }

    private ArticleBodyVo findArticleBody(Integer articleId) {
        LambdaQueryWrapper<ArticleBody> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleBody::getArticleId,articleId);
        ArticleBody articleBody = articleBodyMapper.selectOne(queryWrapper);
        ArticleBodyVo articleBodyVo = new ArticleBodyVo();
        articleBodyVo.setContent(articleBody.getContent());
        articleBodyVo.setContentHtml(articleBody.getContentHtml());
        return articleBodyVo;
    }
}
